package com.atguigu.flink.timer;

import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2022/11/23

 */
public class Demo3_EventTimeTimer
{
    public static void main(String[] args) {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.getConfig().setAutoWatermarkInterval(2000);

        env.setParallelism(1);

        //水印策略
        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
            .<WaterSensor>forMonotonousTimestamps()   //目前的场景数据都是有序的
                                                      //如何产生水印
                                                      .withTimestampAssigner((e, r) -> e.getTs());

        env
           .socketTextStream("hadoop103", 8888)
           .map(new MapFunction<String, WaterSensor>()
           {
               @Override
               public WaterSensor map(String value) throws Exception {
                   String[] data = value.split(",");
                   return new WaterSensor(
                       data[0],
                       Long.valueOf(data[1]),
                       Integer.valueOf(data[2])
                   );
               }
           })
           .assignTimestampsAndWatermarks(watermarkStrategy)
           .keyBy(WaterSensor::getId)
           .process(new KeyedProcessFunction<String, WaterSensor, String>()
           {
               //定时器触发后的执行逻辑   OnTimerContext ctx, Collector<String> out： 和processElement()中的out和ctx是同一个对象
               @Override
               public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                   out.collect(timestamp + "定时器被触发了,预警...");
               }

               long ts ;
               @Override
               public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                   TimerService timerService = ctx.timerService();

                   if (value.getVc() > 30) {
                       //eventtime从数据中抽取时间属性
                       ts = ctx.timestamp() + 5000;
                       System.out.println("注册了定时器:"+ts);
                       timerService.registerEventTimeTimer(ts);
                   }else if (value.getVc() < 10){
                       System.out.println("删除了定时器:"+ts);
                       timerService.deleteEventTimeTimer(ts);
                   }

                   out.collect(value.toString());
               }
           })
           .print();

        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }
}
